package com.ext.aic.utils;

import android.content.Context;

import androidx.annotation.ColorInt;

import com.ext.aic.R;
import com.eyinfo.android_pure_utils.events.Action0;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.impl.ConfirmPopupView;
import com.lxj.xpopup.interfaces.OnConfirmListener;

public class XPopupUtils {
    private static volatile XPopupUtils instance;

    public static XPopupUtils getInstance() {
        if (instance == null) {
            synchronized (XPopupUtils.class) {
                if (instance == null) {
                    instance = new XPopupUtils();
                }
            }
        }
        return instance;
    }

    private ConfirmPopupView confirmPopupView;
    private BasePopupView customPopupView;
    private BasePopupView customDialogView;

    public <T extends BasePopupView> void showBottomDialog(Context context, T popup, @ColorInt int shadowBgColor) {
        if (customPopupView != null && customPopupView.isShow()) {
            customPopupView.dismiss();
            customPopupView = null;
        }

        XPopup.Builder builder = new XPopup.Builder(context);
        builder.isDestroyOnDismiss(true);
        builder.isViewMode(true);
        builder.shadowBgColor(shadowBgColor);
        customPopupView = builder.asCustom(popup);
        customPopupView.show();
    }

    public <T extends BasePopupView> void showBottomDialog(Context context, T popup) {
        showBottomDialog(context, popup, 0);
    }

    private static class ConfirmCall implements OnConfirmListener {

        private Action0 confirmCall;

        public ConfirmCall(Action0 confirmCall) {
            this.confirmCall = confirmCall;
        }

        @Override
        public void onConfirm() {
            if (confirmCall != null) {
                confirmCall.call();
            }
        }
    }

    private XPopup.Builder getDialogBuilder(Context context, boolean autoDismiss) {
        XPopup.Builder builder = new XPopup.Builder(context);
        builder.isDestroyOnDismiss(true);
        builder.autoDismiss(autoDismiss);
        builder.isViewMode(false);
        builder.dismissOnBackPressed(false);
        builder.dismissOnTouchOutside(false);
        return builder;
    }

    public ConfirmPopupView showConfirmDialog(Context context, CharSequence title, CharSequence content, boolean isHideCancel, String cancelText, String confirmText, boolean autoDismiss, Action0 confirmCall) {
        if (confirmPopupView != null && confirmPopupView.isShow()) {
            confirmPopupView.dismiss();
            confirmPopupView = null;
        }
        XPopup.Builder builder = getDialogBuilder(context, autoDismiss);
        confirmPopupView = builder.asConfirm(title, content,
                cancelText, confirmText,
                new ConfirmCall(confirmCall), null, isHideCancel);
        confirmPopupView.show();
        return confirmPopupView;
    }

    public ConfirmPopupView showConfirmDialog(Context context, CharSequence title, CharSequence content, boolean isHideCancel, boolean autoDismiss, Action0 confirmCall) {
        String cancel = context.getString(R.string.cancel);
        String confirm = context.getString(R.string.confirm);
        return showConfirmDialog(context, title, content, isHideCancel, cancel, confirm, autoDismiss, confirmCall);
    }

    public ConfirmPopupView showConfirmDialog(Context context, CharSequence title, CharSequence content, boolean isHideCancel, Action0 confirmCall) {
        return showConfirmDialog(context, title, content, isHideCancel, true, confirmCall);
    }

    public <T extends BasePopupView> void showCustomDialog(Context context, T popup) {
        if (customDialogView != null && customDialogView.isShow()) {
            customDialogView.dismiss();
            customDialogView = null;
        }
        XPopup.Builder builder = getDialogBuilder(context, true);
        customDialogView = builder.asCustom(popup);
        customDialogView.show();
    }
}
